#include "page_manager.h"

static lv_page_t* lv_page_manager_head;     //lv_manager表头
static int last_key = ROOT;                 //记录上一次显示的key值

void lv_page_entry_default(lv_obj_t *obj)
{
    printf("page_entry_default enter\n");
    lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);
}

void lv_page_exit_default(lv_obj_t* obj)
{
    printf("page_ exit_default exit\n");
    lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
}


void lv_page_manager_add_node(lv_page_t* page_node)
{
    lv_page_t* page_manager_tail = lv_page_manager_head->pri;
    page_node->next = page_manager_tail->next;
    page_node->pri = page_manager_tail;
    page_manager_tail->next = page_node;
   lv_page_manager_head->pri = page_node;
}

void lv_page_manager_create(lv_obj_t* parent)
{
    lv_obj_t* obj = lv_obj_create(parent);
    lv_obj_set_pos(obj,0,0);
    lv_obj_remove_style_all(obj);
    lv_obj_set_size(obj, lv_obj_get_width(parent), lv_obj_get_height(parent));
    if (lv_page_manager_head == NULL)
    {
        lv_page_manager_head = (lv_page_t*)malloc(sizeof(lv_page_t));
        if (lv_page_manager_head == NULL)
        {
            printf("lv_page_manage_head malloc mem failed\n");
            return;
        }
        lv_page_manager_head->page.obj = obj;
        lv_page_manager_head->page.key = ROOT;
        lv_page_manager_head->ops.create = NULL;
        lv_page_manager_head->ops.destroy = NULL;
        lv_page_manager_head->ops.entry = NULL;
        lv_page_manager_head->ops.exit = NULL;
        lv_page_manager_head->pri = lv_page_manager_head->next = lv_page_manager_head;
    }
}

void lv_page_create(int page_key, void(*create), void(*destroy), void(*entry), void(*exit))
{
    lv_obj_t* obj = lv_obj_create(lv_page_manager_head->page.obj);

    lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
    lv_page_t* page_node = (lv_page_t*)malloc(sizeof(lv_page_t));
    if (page_node == NULL) {
        printf("lv_page_manage_node malloc mem failed\n");
        return;
    }

    page_node->page.obj = obj;
    page_node->page.key = page_key;

    page_node->ops.create = create;
    page_node->ops.destroy = destroy;
    if (entry == NULL)
    {
        page_node->ops.entry = lv_page_entry_default;
    }
    else {
        page_node->ops.entry = entry;
    }

    if (exit == NULL) {
        page_node->ops.exit = lv_page_exit_default;
    }
    else {
        page_node->ops.exit = exit;
    }
    lv_page_manager_add_node(page_node);
}

void lv_page_remove(int page_key) {
    lv_page_t* page_node = lv_get_page(page_key);
    if (page_node == NULL) {
        printf("warning:key:%d page is not exit", page_key);
        return;
    }
    if (page_node->ops.destroy == NULL){
        printf("warning:key:%d destroy func is not exit", page_key);
    }
    else {
        page_node->ops.destroy(page_node->page.obj);
    }

    lv_page_manager_remove_node(page_key);
}


void lv_page_manager_remove_node(int page_key)
{
    lv_page_t* page_node = lv_get_page(page_key);
    printf("lv_page_manager_remove_node:%d,obj = %p\n", page_key, page_node->page.obj);
    lv_page_t* node_priv = page_node->pri;
    lv_page_t* node_next = page_node->next;
    node_priv->next = node_next;
    node_next->pri = node_priv;
    free(page_node);
    page_node = NULL;
}

lv_page_t* lv_get_page(int page_key)
{
    lv_page_t* lv_page_head = lv_page_manager_head->next;

    while (lv_page_head->page.key != ROOT)
    {
        if (lv_page_head->page.key == page_key)
            return lv_page_head;
        lv_page_head = lv_page_head->next;
    }
    return NULL;
}

void lv_page_bind_create(int page_key, void (*page_create))
{
    lv_page_t* page_node = lv_get_page(page_key);
    if (page_node == NULL)
    {
        printf("node not found");
        return;
    }
    page_node->ops.create = page_create;
}

void lv_page_bind_destroy(int page_key, void(*page_destroy))
{
    lv_page_t* page_node = lv_get_page(page_key);
    if (page_node == NULL)
        printf("node not found");
}

void lv_page_bind_entry(int page_key, void(*entry))
{
    lv_page_t* page_node = lv_get_page(page_key);
    if (page_node == NULL)
        printf("node not found");
    if (entry == NULL) {
        page_node->ops.entry = lv_page_entry_default;
    }
    else {
        page_node->ops.entry = entry;
    }
}

void lv_page_bind_exit(int page_key, void(*exit))
{
    lv_page_t* page_node = lv_get_page(page_key);
    if (page_node == NULL)
        printf("node not found");
    if (exit == NULL) {
        page_node->ops.exit = lv_page_exit_default;
    }
    else {  
        page_node->ops.exit = exit;
    }
}


void lv_page_show(void)
{
    lv_page_t* page_manager_head = lv_page_manager_head->next;
    while (page_manager_head->page.key != ROOT)
    {
        printf("\nlv_page_show page key = %d ,page obj = %p\n", page_manager_head->page.key, page_manager_head->page.obj);
        page_manager_head = page_manager_head->next;
    }
}


void lv_main_page_start_up(void)
{
    lv_page_t* page_manager_head = lv_page_manager_head->next;
    while (page_manager_head->page.key != ROOT) {
        if (page_manager_head->ops.create == NULL)
            continue;

        page_manager_head->ops.create(page_manager_head->page.obj);
        page_manager_head = page_manager_head->next;
    }
}


void lv_page_load(int page_key)
{
    if (last_key != ROOT)
    {
        lv_page_t* last_node = lv_get_page(last_key);
        if (last_node != NULL) {
            if (last_node->ops.exit == NULL)
            {
                lv_page_exit_default(last_node->page.obj);
            }
            else
            {
                last_node->ops.exit(last_node->page.obj);
            }
        }
    }

    lv_page_t* node = lv_get_page(page_key);
    if (node != NULL)
    {
        last_key = page_key;
        node->ops.entry(node->page.obj);
    }
}
